<?php

namespace Admin\Controller;

class OrderLogController extends AdminController {

    public function index() {

        $is_export = I("is_export", 0, 'intval');
        $where .= 'order_status=50 ';

        $keywords = I('keywords');
        if ($keywords) {
            $where .= " and (o.order_no like '%" . $keywords . "%' or o.pick_no like '%" . $keywords . "%')";
            $this->assign('keywords', $keywords);
        }


        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");


        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where .= " and o.create_time between '$start_date 00:00:00' AND " . "'" . $end_date . ' 23:59:59' . "'";
//                $where['sp.create_time'] = array("between", array($start_date, $end_date . ' 23:59:59'));
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
//                $where['sp.create_time'] = array('EGT', $start_date);
                $where .= " and o.create_time >= '$start_date 00:00:00'";
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
//                $where['sp.create_time'] = array('ELT', $end_date . ' 23:59:59');
                $where .= ' and o.create_time <=' . "'" . $end_date . '23:59:59' . "'";
                $this->assign("end_date", $end_date);
            }
        }
        $box['small_carpool'] = '小柜拼车';
        $box['small_single'] = '小柜单放';
        $box['big_cabinet'] = '大柜';
        $box['tall_cabinet'] = '高柜';
        $box['super_tall_cabinet'] = '超高柜';

        $M = M()->table("ot_order o")
                ->join('ot_company c on o.company_id = c.company_id')
                ->join('ot_shipment_address sa on o.shipment_address_id = sa.shipment_address_id')
                ->join('left join ot_dock_weight dw on o.weight_id=dw.weight_id');


        if ($is_export) {
            $exportList = $M->field('o.order_id,'
                            . 'CONCAT(o.order_no," ") as order_no,'
                            . 'c.company_name,'
                            . 'CONCAT(o.pick_no," ") as pick_no,'
                            . 'o.order_type,'
                            . 'o.shipment_time,'
                            . 'o.cutoff_time,'
                            . 'CONCAT(sa.province,sa.city,sa.address,sa.address_desc) as shipment_address_name,'
                            . 'dw.weight_desc,'
                            . 'o.order_price,'
                            . 'o.driver_price,'
                            . 'o.create_time,'
                            . 'o.other_price')->where($where)->select();

            if (!empty($exportList)) {
                foreach ($exportList as &$val) {
                    $val['order_type'] = $box[$val['order_type']];
                }
                unset($val);
            }

            exportExcel('订单流水导出', array('订单ID', '订单号', '下单企业', '提单号', '箱型', '装箱时间', '截关时间', '装箱点', '货重', '运费', '司机运费', '下单时间', '额外费用'), $exportList);
//            exportExcel('消费卡导出', array('编号', '名称', '卡号', '密码', '卡片金额', '状态(0正常 1已消费)', '生成时间', '有效期'), $exportList);
        }
        $field = 'o.*,c.company_name,sa.province,sa.city,sa.address,sa.address_desc,dw.weight_desc';

        $order = "o.order_id desc";
        $list = $this->lists($M, $where, $order, $field);



        $this->assign('box', $box);
        $this->assign('_list', $list);
        $this->display();
    }

}
